package com.geologyoa.persist;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.geologyoa.commons.JdbcUtil;
import com.geologyoa.commons.commUtils;
import com.geologyoa.domain.Function;
import com.geologyoa.domain.Power;
import com.geologyoa.domain.RoleAndPow;

public class PowerDaoImpl implements PowerDao {
	Connection conn = null;
	PreparedStatement ps = null;
	ResultSet rs = null;
	public Boolean AddPower(Power power) {
		Boolean flag =false;
		   conn=JdbcUtil.getConnection();
		   String sql = "";
		   sql="select * from power where roleid='"+power.getRoleid()+"' and functionid= '"+power.getFunctionid()+"'";
		//  sql = "insert into power values(oa_seq.nextval,?,?,?)";
			try {
				ps = conn.prepareStatement(sql);
				ResultSet rs = ps.executeQuery();
				if(rs.next()){
					sql="update power set rolefunction='"+power.getRolefunction()+"' where roleid='"+power.getRoleid()+"' and functionid= '"+power.getFunctionid()+"'";
					ps = conn.prepareStatement(sql);
					int number=ps.executeUpdate();
					if(number>0)
					{
						flag=true;
					}
					
				}else{
					sql = "insert into power values(oa_seq.nextval,?,?,?)";
					ps = conn.prepareStatement(sql);
					ps.setInt(1, power.getRoleid());
					ps.setInt(2, power.getFunctionid());
					ps.setString(3, power.getRolefunction());
					int number=ps.executeUpdate();
					if(number>0)
					{
						flag=true;
					}
				}
			} catch (SQLException e) {
				e.printStackTrace();
			} finally
			{
				JdbcUtil.closeAll(ps,conn);
			}
			return flag;
	}
	public List SerachPower(Integer roleid) {
		RoleAndPow roleAndPow=null;
		List<RoleAndPow> roleandpowList=new ArrayList<RoleAndPow>();
		   conn=JdbcUtil.getConnection();
		   String sql = "";
		   sql = "select r.rolename,p.rolefunction,f.functionname,f.functionid from power p,roles r,function f where p.roleid=r.roleid and p.functionid=f.functionid and p.roleid=?";
			try {
				ps = conn.prepareStatement(sql);
				ps.setInt(1, roleid);
				rs=ps.executeQuery();
				while(rs.next())
				{
					roleAndPow=new RoleAndPow();
					roleAndPow.setRolenames(rs.getString("rolename"));
					roleAndPow.setFunctionname(rs.getString("functionname"));
					roleAndPow.setRolefunction(rs.getString("rolefunction"));
					roleAndPow.setFunctionid(rs.getString("functionid"));
					roleandpowList.add(roleAndPow);
				}
				
			} catch (SQLException e) {
				e.printStackTrace();
			} finally
			{
				JdbcUtil.closeAll(ps,conn);
			}
			return roleandpowList;
	}
	public List<Power> SearchFunction(Integer roleid, Integer functionid) {
		Power power=null;
		List<Power> powerList=new ArrayList<Power>();
		   conn=JdbcUtil.getConnection();
		   String sql = "";
		   sql="select * from power where roleid='"+roleid+"' and functionid= '"+functionid+"'";
			try {
				ps = conn.prepareStatement(sql);
				rs=ps.executeQuery();
				while(rs.next())
				{
					power=new Power();
					power.setRolefunction(rs.getString("rolefunction"));
					powerList.add(power);
				}
				
			} catch (SQLException e) {
				e.printStackTrace();
			} finally
			{
				JdbcUtil.closeAll(ps,conn);
			}
			return powerList;
	}
}
